package game.model.impl

import game.Config
import game.business.*
import game.enums.Direction
import game.model.View
import org.itheima.kotlin.game.core.Painter

/**
 * 坦克
 *
 * 具备移动能力
 * 具备阻挡能力
 * 具备被攻击能力
 * 具备销毁能力
 */
class Tank(override var x: Int, override var y: Int) : Movable,BlockAble, SufferAble,DestroyAble{

    override var width: Int = Config.block
    override var height: Int = Config.block

    //坦克血量
    override var blood: Int = Config.tankBlood

    //方向
    override var currentDirection: Direction = Direction.up

    //速度
    override val speed = Config.tankSpeed
    //阻挡方向
    private var badDirection: Direction? = null


    override fun draw() {

        //根据坦克的方向进行绘制
        //方式一
//        when(currentDirection){
//            Direction.up -> Painter.drawImage("img/tank/tankU.gif",x,y)
//            Direction.down -> Painter.drawImage("img/tank/tankD.gif",x,y)
//            Direction.left -> Painter.drawImage("img/tank/tankL.gif",x,y)
//            Direction.right -> Painter.drawImage("img/tank/tankR.gif",x,y)
//        }

        //方式二
        var imagePath = when (currentDirection) {
            Direction.up -> "img/tank_u.gif"
            Direction.down -> "img/tank_d.gif"
            Direction.left -> "img/tank_l.gif"
            Direction.right -> "img/tank_r.gif"
        }
        Painter.drawImage(imagePath, x, y)
    }

    /**
     * 坦克移动
     */
    fun move(direction: Direction) {
        //判断是否要往碰撞的方向运动
        if (direction == this.badDirection)
            return

        if (this.currentDirection != direction) {
            this.currentDirection = direction
            return
        }

        when (currentDirection) {
            Direction.up -> y -= speed
            Direction.down -> y += speed
            Direction.left -> x -= speed
            Direction.right -> x += speed
        }

        if (x < 0) x = 0
        if (x > Config.gameWidth - width) x = Config.gameWidth - width
        if (y < 0) y = 0
        if (y > Config.gameHeight - height) y = Config.gameHeight - height
    }

/*    override fun willCollision(block: BlockAble): Direction? {
//        //未来的坐标
//        var x: Int = this.x
//        var y: Int = this.y
//        //将要碰撞时去做判断
//        when (currentDirection) {
//            Direction.up -> y -= speed
//            Direction.down -> y += speed
//            Direction.left -> x -= speed
//            Direction.right -> x += speed
//        }

        //TODO:碰撞检测
//        var collision = when {
//            block.y + block.height <= y -> {
//                //如果 阻挡物 在运动物的上方的时候，不碰撞
//                false
//            }
//            y + height <= block.y -> {
//                //如果 阻挡物 在运动物的下方的时候，不碰撞
//                false
//            }
//            block.x + block.x <= x -> {
//                //如果 阻挡物 在运动物的左方的时候，不碰撞
//                false
//            }
//            else -> x + width > block.x
//        }

        var collision = checkCollision(block)

        return if (collision) currentDirection else null
    }*/

    override fun notifyCollision(direction: Direction?, block: BlockAble?) {
        //TODO: 接收到碰撞信息
        this.badDirection = direction
    }

    /**
     * 发射子弹的方法
     */
    fun shot(): Bullet {
        var x = this.x
        var y = this.y
        when(currentDirection){
            Direction.up -> {
                x += Config.block / 2 - 7
                y -= Config.block / 2 - 8
            }
            Direction.down -> {
                x += Config.block / 2 - 7
                y += Config.block - 8
            }
            Direction.right -> {
                x += Config.block - 8
                y += Config.block / 2  - 8
            }
            Direction.left -> {
                y += Config.block / 2 - 8
                x -= Config.block / 2 - 8
            }
        }
        return Bullet(x, y, currentDirection,this)
    }

    override fun notifySuffer(attackAble: AttackAble): Array<View>? {
        blood -= attackAble.attackPower
        return arrayOf(Blast(x,y))
    }

    override fun isDestroyed(): Boolean = blood <= 0
}